//
//  CZMainViewController.swift
//  CZVVeiBo
//
//  Created by smile on 2017/7/29.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 创建自定义的tabbar
        let tabbar = CZTabBar()
        // 02-实例化闭包
        tabbar.closure = {
            // 04-闭包回调
            CZComposeView.show(target: self)
        }
        // KVC 给只读属性赋值
        setValue(tabbar, forKey: "tabBar")
        addChildViewController(vc: CZHomeViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(vc: CZMessageViewController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(vc: CZDiscoverViewController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(vc: CZProfileViewController(), title: "我", imgName: "tabbar_profile")
        
    }
    
    // 备注快捷:option+command+/
    /// 添加子控制器快捷方式
    ///
    /// - Parameters:
    ///   - vc: 对象
    ///   - title: title
    ///   - imgName: 图片名
    private func addChildViewController(vc: UIViewController, title: String, imgName: String){
        // 设置title
        vc.title = title
        // 设置图片
        // 采用图片的原生颜色alwaysOriginal
        vc.tabBarItem.image = UIImage(named: imgName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        // 设置tabBarItem的title颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.selected)
        // 创建导航控制器
        let nav = CZNavigationController(rootViewController: vc)
        // 添加子控制器
        addChildViewController(nav)
    }
    
    

}
